#include "CfgReader.h"




std::map<std::string, std::string> CfgReader::readConfig(char * fileName){

	std::map<std::string,	std::string>	values;

	std::ifstream myfile (fileName);
	bool lika = false;
	

	if (myfile.is_open())
	{
		std::string line, value, variable;

		while (getline(myfile,line))
		{
			if(line.length() > 0){

				value = variable = "";

				std::string::iterator it = line.begin();

				if((*it) != '['){

					while(it != line.end()){

						if((*it) == '='){
							lika = true;

						}else if((*it) != ' '){
							
							if(lika){

								value += (*it);

							}else{

								variable += (*it);

							}
						}

						it++;

					}
				}

				if(lika){
					values[variable] = value;

				}
				
				lika = false;
			}

		}
    myfile.close();

	}else std::cout << "Unable to open file";

	return values;

};



int	 CfgReader::strToInt(std::string in){

	return atoi(in.c_str());

}

double	CfgReader::strToDouble(std::string in){

	return atof(in.c_str());

}

std::vector<bigTile> * CfgReader::mapToBigTile(std::map<std::string, std::string> &inMap){

	std::map<std::string, std::string>::iterator it = inMap.begin();
	std::vector<bigTile> *tiles = new std::vector<bigTile>();
	std::string temp = "";
	std::string num = "";

	bigTile tempTile;
	std::string::iterator itStr;

	while(it != inMap.end()){

		temp = (*it).second;
		tempTile.id = strToInt((*it).first);
		itStr = temp.begin();
		int i = 0;
		num = "";	

		while(itStr != temp.end()){

			if((*itStr) != ',' && (*itStr) != ';'){
				num += (*itStr);
			}else{
				
				if(i == 0)tempTile.path	= "..\\..\\data\\" + num;	
				if(i == 1)tempTile.passable  = strToInt(num);
				if(i == 2) tempTile.difficulty	= strToDouble(num);

				num = "";
				i++;
								

			}

			
			itStr++;

		}
		tiles->push_back(tempTile);
		it++;
	}

	return tiles;


}


std::vector<smallTile> * CfgReader::mapToSmallTile(std::map<std::string, std::string> &inMap){

	std::map<std::string, std::string>::iterator it = inMap.begin();
	std::vector<smallTile> *tiles = new std::vector<smallTile>();
	std::string temp = "";
	std::string num = "";

	smallTile tempTile;
	std::string::iterator itStr;

	while(it != inMap.end()){

		temp = (*it).second;
		tempTile.id = strToInt((*it).first);
		itStr = temp.begin();
		int i = 0;
		int a = 0;
		num = "";	

		while(itStr != temp.end()){

			if((*itStr) != ',' && (*itStr) != ';'){
				num += (*itStr);
			}else{
				
				if(i == 0)tempTile.path	= "..\\..\\data\\" + num;
				if(i == 1)tempTile.level  = strToInt(num);
				if(i == 2)tempTile.passable  = strToInt(num);
				if(i >= 3){				
					tempTile.difficulty[a] = strToDouble(num);
					++a;
				}

				num = "";
				i++;
								

			}

			
			itStr++;

		}

		while(a < MAX_TILE_SIZE*MAX_TILE_SIZE){

			tempTile.difficulty[a] = -1;
			++a;
		}
		

		tiles->push_back(tempTile);
		it++;
	}

	std::sort(tiles->begin(),tiles->end(),compare);

	return tiles;


}

bool CfgReader::compare (const smallTile& a, const smallTile& b){


	return (a.id < b.id);
}
